package com.example.demo_yunpan.mapper;

import com.example.demo_yunpan.entity.folder;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface FolderMapper {
    /**
     * userId查询第一级文件夹
     * @param userId
     * return 查询结果
     * */
    @Select("select * from folder WHERE user_id = #{userId} and parent_hdfs = #{path}")
    List<folder> selectFolderByuserId(@Param("userId") String userId,@Param("path")String path);

    /**
     * 查询同一个用户Id的所有文件夹
     * @param userId,fileNameFuzzy,category
     * return List<folder>
     * */
    @Select("<script>"+
            "select * from folder where user_id = #{userId}\n" +
            "        <if test=\"fileNameFuzzy != null and fileNameFuzzy != ''\">\n" +
            "            and folder_name like CONCAT('%', #{fileNameFuzzy}, '%')\n" +
            "        </if>"+
            "</script>")
    List<folder> selectAllByuserId(@Param("userId") String userId,@Param("fileNameFuzzy")String fileNameFuzzy,@Param("category")String category);

    /**
     * folderId查询文件夹
     * @param folderId
     * return 查询结果
     * */
    @Select("select * from folder where folder_id = #{folderId}")
    folder selectFolderByFolderId(@Param("folderId") String folderId);

    /**
     * 返回上一级文件夹
     * @param folderId
     * return 查询结果
     * */
    @Select("select * from folder\n" +
            "        where folder_id = (\n" +
            "            select last_folder from folder where last_folder is not null\n" +
            "                                             and folder_id = #{folderId}\n" +
            "            )")
    folder selectLastFolderByFolderId(@Param("folderId") String folderId);

    /**
     * 新建文件夹
     * @param folder
     * return 插入结果
     * */
    @Insert("insert into folder (folder_name,last_folder,last_folder_name,user_id,create_time,folder_size,hdfs_path) " +
            "values (#{folderName},#{lastFolder},#{lastFolderName},#{userId},#{createTime},#{folderSize},#{hdfsPath});")
    boolean insertNewFolder(folder folder);

    /**
     * 删除文件夹
     * @Param folderId
     * return
     * */
    @Delete("delete from folder where folder_id = #{folderId}")
    boolean delectFolderByFolderId(@Param("folderId")String folderId);

    /**
     * 更新文件夹信息
     * @Param folder
     * return
     * */
    boolean updataFolderInfo(folder folder);

    /**
     * 查询同一级下是否存在重复的文件名
     * @param Name
     * @param lastFolder
     * return 查询结果
     * */
    @Select("<script>" +
            "select * from folder where folder_name = #{folderName} " +
            "        <if test=\"lastFolder != null\">\n" +
            "            and last_folder = #{lastFolder}\n" +
            "        </if>"+
            "</script>")
    folder selecFolderByName(@Param("lastFolder")int lastFolder,@Param("folderName")String Name);

    /**
     * 根据文件夹ID查询文件夹信息
     * @param folderId
     * return 查询结果
     * */
    @Select("select * from folder where folder_id = #{folderId}")
    folder selecFolderById(String folderId);
}
